package org.gdstash.quest;

import org.gdstash.character.GDCharUID;
import org.gdstash.file.GDReader;
import org.gdstash.util.FileVersionException;
import org.gdstash.util.GDMsgFormatter;
import org.gdstash.util.GDMsgLogger;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class GDQuestFile {
    private File file;
    private int key;
    private int version;
    private boolean fileError;
    private GDCharUID id;
    private GDQuestTokenList tokens;
    private GDQuestList quests;

    public GDQuestFile(final File file) {
        this.file = file;
        this.fileError = false;
        this.id = new GDCharUID();
        this.tokens = new GDQuestTokenList();
        this.quests = new GDQuestList();
    }

    public void read() {
        String path = null;
        try {
            path = this.file.getCanonicalPath();
        } catch (IOException ex4) {
        }
        int val = 0;
        try {
            GDReader.readEncFileToBuffer(this.file);
            this.key = GDReader.readKey();
            val = GDReader.readEncInt(true);
            if (val != 1481921361) {
                throw new FileVersionException(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_UNSUPPORTED_VERSION"));
            }
            this.version = GDReader.readEncInt(true);
            if (this.version != 0) {
                throw new FileVersionException(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_UNSUPPORTED_VERSION"));
            }
            this.id.read();
            this.tokens.read();
            this.quests.read();
            this.tokens.listTokens();
        } catch (FileNotFoundException ex) {
            if (path != null) {
                final Object[] args = {path};
                final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_FILE_NOT_FOUND", args);
                GDMsgLogger.addError(msg);
            }
            GDMsgLogger.addError(ex);
            this.fileError = true;
        } catch (FileVersionException ex2) {
            if (path != null) {
                final Object[] args = {path};
                final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_FILE_READ", args);
                GDMsgLogger.addError(msg);
            }
            GDMsgLogger.addInfo(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "INFO_UPDATE_GDSTASH"));
            GDMsgLogger.addError(ex2);
            this.fileError = true;
        } catch (Exception ex3) {
            if (path != null) {
                final Object[] args = {path};
                final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_FILE_READ", args);
                GDMsgLogger.addError(msg);
            }
            GDMsgLogger.addError(ex3);
            this.fileError = true;
        }
    }
}
